package edu.chl.grupp11.aegas.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.sanselan.formats.tiff.constants.TiffFieldTypeConstants;

import edu.chl.grupp11.aegas.model.AbstractMetaTag;
import edu.chl.grupp11.aegas.model.imp.ImmutableMetaTag;
import edu.chl.grupp11.aegas.model.imp.MutableJpgMetaTag;

public class TagView extends JPanel implements FocusListener, Observer,
		Comparable<TagView> {
	private static final long serialVersionUID = 1L;

	public static TagView getTagWithSameName(ArrayList<TagView> tagList,
			TagView tag) {
		for (TagView t : tagList) {
			if (t.getName().equalsIgnoreCase(tag.getName())) {
				return t;
			}
		}
		return null;
	}

	public static boolean hasTag(ArrayList<TagView> tagList, TagView tag) {
		for (TagView t : tagList) {
			if (t.getName().equalsIgnoreCase(tag.getName())) {
				return true;
			}
		}
		return false;
	}

	public static boolean sameValue(TagView t1, TagView t2) {
		return t1.getValue().equals(t2.getValue());
	}

	private JLabel nameLabel = new JLabel();

	protected JTextField valueTextField = new JTextField();

	private AbstractMetaTag metaTag;

	private boolean hasChanged;

	public TagView(AbstractMetaTag tag) {
		if (tag instanceof ImmutableMetaTag) {
			valueTextField.setEnabled(false);
		}

		nameLabel.setText(tag.getName());
		valueTextField.setText(tag.getValue());
		metaTag = tag;

		tag.addObserver(this);

		setLayout(new GridLayout(1, 2));
		setPreferredSize(new Dimension(530, 30));
		add(nameLabel);
		add(valueTextField);
		valueTextField.addFocusListener(this);

		setBackground(new Color(196, 199, 200));
	}

	/**
	 * @return 0 if equal, 1 if not equal
	 */
	@Override
	public int compareTo(TagView tag) {
		if (getName().equalsIgnoreCase(tag.getName())
				&& getValue().equalsIgnoreCase(tag.getValue())) {
			return 0;
		}
		return 1;
	}

	@Override
	public void focusGained(FocusEvent arg0) {
		// Do nothing
	}

	@Override
	public void focusLost(FocusEvent arg0) {
		setChanged();
	}

	public AbstractMetaTag getIMetaTag() {
		return metaTag;
	}

	@Override
	public String getName() {
		return nameLabel.getText();
	}

	public String getValue() {
		return valueTextField.getText();
	}

	public boolean hasChanged() {
		if (hasChanged) {
			hasChanged = false;
			return true;
		}
		return false;
	}

	public boolean hasValidUserInput() {
		try {
			if (metaTag instanceof MutableJpgMetaTag) {
				MutableJpgMetaTag tempTag = (MutableJpgMetaTag) metaTag;
				if (!tempTag
						.arrayOnlyContains(TiffFieldTypeConstants.FIELD_TYPE_ASCII)) {
					Double.parseDouble(valueTextField.getText());
				}
			}
		} catch (NumberFormatException e) {
			return false;
		}
		return true;
	}

	public void setChanged() {
		hasChanged = true;
	}

	private void setFieldName(String name) {
		nameLabel.setText(name);
	}

	private void setFieldValue(String value) {
		valueTextField.setText(value);
	}

	public void setTemporaryText(String s) {
		valueTextField.setText(s);
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		this.setFieldName(metaTag.getName());
		this.setFieldValue(metaTag.getValue());
	}
}
